import 'package:casdon/common/utils/string_externtion.dart';
import 'package:casdon/modules/lifeCircle/data/api/life_topic_list_api.dart';
import 'package:casdon/modules/recipe/data/model/recipe_placeholder_data.dart';

enum LifeTopicType { official, user, video, unknow }

enum LifeTopicMediaType { text, image, video, unknow }

enum LifeTopicCheckState { await, success, failed }

enum LifeTopicPublishState { no, yes }

class LifeTopicModule {
  final String id;

  final String userId;
  final String avatar;
  final String userNickname;
  final LifeTopicType type; //帖子类型(0:官方帖子;1:用户帖子;2:品牌视频)
  final String title;
  final String content; // 官方帖子为富文本，用户帖子为纯文本
  final String coverImg; // 封面图
  final List<String> mediaSrcList; // [媒体ID]
  final LifeTopicMediaType mediaType; // 0=纯文本，1=图片，2=视频
  final int views; // 浏览量
  final int comments; // 评论量
  final int likes; // 点赞量
  final bool isLike; // 是否点赞
  final bool isFollow; // 是否关注
  final bool isMyTopic; // 是否我的帖子
  final int status; // (0:启用（展示）,1:禁用（隐藏）)
  final LifeTopicPublishState publishStatus; //帖子发布状态（0=未发布，1=已发布）
  final LifeTopicCheckState checkStatus; // 审核状态 0待审核 1审核通过 2审核不通过

  final String createTime;

  LifeTopicModule({
    required this.id,

    required this.userId,
    required this.avatar,
    required this.userNickname,
    required this.type,
    required this.title,
    required this.content,
    required this.coverImg,
    required this.mediaSrcList,
    required this.mediaType,
    required this.views,
    required this.comments,
    required this.likes,
    required this.isLike,
    required this.isFollow,
    required this.isMyTopic,
    required this.status,
    required this.publishStatus,
    required this.checkStatus,

    required this.createTime,
  });

  // 静态方法转换
  static LifeTopicModule fromApi(LifeTopic response) {
    return LifeTopicModule(
      id: response.id,
      userId: response.userId,
      type: _convertType(response.type),
      avatar: response.avatar ?? "",
      userNickname: response.userNickname ?? "匿名用户",
      title: response.title ?? "",
      content: (response.content ?? "").cropParams700,
      coverImg: response.coverImg ?? recipePlaceHolderImgUrl,
      mediaSrcList: convertMediaSrc(response.mediaSrc),
      mediaType: _convertMediaType(response.mediaType),
      views: response.views ?? 0,
      comments: response.comments ?? 0,
      likes: response.likes ?? 0,
      isLike: response.isLike ?? false,
      isFollow: response.isFollow ?? false,
      isMyTopic: response.isMyTopic ?? false,
      status: response.status ?? 0,
      publishStatus: _convertPublishState(response.publishStatus ?? 0),
      checkStatus: _convertCheckState(response.checkStatus ?? 0),
      createTime: response.createTime ?? "",
    );
  }

  static LifeTopicType _convertType(int? type) {
    switch (type) {
      case 0:
        return LifeTopicType.official;
      case 1:
        return LifeTopicType.user;
      case 2:
        return LifeTopicType.video;
      default:
        return LifeTopicType.unknow;
    }
  }

  static LifeTopicMediaType _convertMediaType(int? type) {
    switch (type) {
      case 0:
        return LifeTopicMediaType.text;
      case 1:
        return LifeTopicMediaType.image;
      case 2:
        return LifeTopicMediaType.video;
      default:
        return LifeTopicMediaType.unknow;
    }
  }

  static LifeTopicPublishState _convertPublishState(int? state) {
    switch (state) {
      case 0:
        return LifeTopicPublishState.no;
      case 1:
        return LifeTopicPublishState.yes;
      default:
        return LifeTopicPublishState.no;
    }
  }

  static LifeTopicCheckState _convertCheckState(int? state) {
    switch (state) {
      case 0:
        return LifeTopicCheckState.await;
      case 1:
        return LifeTopicCheckState.success;
      case 2:
        return LifeTopicCheckState.failed;
      default:
        return LifeTopicCheckState.await;
    }
  }

  LifeTopicModule copyWith({
    String? id,
    String? userId,
    String? avatar,
    String? userNickname,
    LifeTopicType? type,
    String? title,
    String? content,
    String? coverImg,
    List<String>? mediaSrcList,
    LifeTopicMediaType? mediaType,
    int? views,
    int? comments,
    int? likes,
    bool? isLike,
    bool? isFollow,
    bool? isMyTopic,
    int? status,
    LifeTopicPublishState? publishStatus,
    LifeTopicCheckState? checkStatus,
    String? createTime,
  }) {
    return LifeTopicModule(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      avatar: avatar ?? this.avatar,
      userNickname: userNickname ?? this.userNickname,
      type: type ?? this.type,
      title: title ?? this.title,
      content: content ?? this.content,
      coverImg: coverImg ?? this.coverImg,
      mediaSrcList: mediaSrcList ?? this.mediaSrcList,
      mediaType: mediaType ?? this.mediaType,
      views: views ?? this.views,
      comments: comments ?? this.comments,
      likes: likes ?? this.likes,
      isLike: isLike ?? this.isLike,
      isFollow: isFollow ?? this.isFollow,
      isMyTopic: isMyTopic ?? this.isMyTopic,
      status: status ?? this.status,
      publishStatus: publishStatus ?? this.publishStatus,
      checkStatus: checkStatus ?? this.checkStatus,
      createTime: createTime ?? this.createTime,
    );
  }

  List<String> imageList() {
    if (LifeTopicMediaType.text == mediaType) {
      return [];
    }

    if (LifeTopicMediaType.image == mediaType) {
      return mediaSrcList;
    }

    if (LifeTopicMediaType.video == mediaType) {
      return [coverImg];
    }

    if (LifeTopicMediaType.unknow == mediaType) {
      return [];
    }

    return [];
  }

  List<String> videoList() {
    if (LifeTopicMediaType.text == mediaType) {
      return [];
    }

    if (LifeTopicMediaType.image == mediaType) {
      return [];
    }

    if (LifeTopicMediaType.video == mediaType) {
      return mediaSrcList;
    }

    if (LifeTopicMediaType.unknow == mediaType) {
      return [];
    }

    return [];
  }

  static List<String> convertMediaSrc(String? src) {
    if (src == null || src.trim().isEmpty) return [];
    List<String> imgUrls = src.split(",");
    imgUrls = imgUrls
        .where((url) => url.isNotEmpty)
        .map((url) => url.cropParams1300) // 每个 url 后追加后缀
        .toList();
    return imgUrls;
  }

  // 批量转换的静态方法
  static List<LifeTopicModule> listFromApi(List<LifeTopic> items) {
    return items.map(fromApi).toList();
  }
}

// 官方默认头像： https://i.ibb.co/V09q9YYv/casdon_header_pic_0.png
// 用户默认头像： https://i.ibb.co/mF5xLQ17/user_header_pic_0.png
